package business;
import java.util.ArrayList;

import data.LMA;
import data.Scheduler;
import data.User;
import data.Volunteer;

/**
 * This is the account class. This class is a container class that hold the collections of volunteer,
 * lma, and scheduler accounts. This class allow for add and removal of account of three types. 
 * 
 * @author Minh Thai
 *
 */
public class Accounts {
	
	/**
	 * The container for Volunteer accounts
	 */
	private ArrayList<Volunteer> volunteerList; 
	
	/**
	 * The container for LMA accounts
	 */
	private ArrayList<LMA> lmaList; 
	
	/**
	 * The container for Schedule class
	 */
	private ArrayList<Scheduler> theScheduler; 
	
	/**
	 * The default constructor for Account class
	 */
	public Accounts() {
		volunteerList = new ArrayList<Volunteer>();
		lmaList = new ArrayList<LMA>();
		theScheduler = new ArrayList<Scheduler>();
	}

	
	/**
	 * This method will add a new user account to Accounts, the username of the new account 
	 * cannot already exist within the user specific account collection
	 * 
	 * @param x the new account to add
	 */
	public void addAccount(User x) {
		if ((x != null) && (x.getUsername() != null) && !x.getUsername().trim().equals("")) {
			if (x instanceof Volunteer) {
				Volunteer v = (Volunteer) x; 
				//contains() utilizes equals() method within user class to identify match
				if (!volunteerList.contains(v)) {
					volunteerList.add(v);
				}
			}else if (x instanceof LMA) {
				LMA l = (LMA) x;
				if (!lmaList.contains(l)) {
					lmaList.add(l);
				}				
			}else if (x instanceof Scheduler) {
				Scheduler s = (Scheduler) x;
				if (!theScheduler.contains(s)) {
					theScheduler.add(s);
				}				
			}
		}
	}
	
	/**
	 * This class allow for the retrieval of user account given the username
	 * 
	 * @return User the account if exist, else null if does not exist within account database
	 */
	public User getAccount(String username) {
		if (username != null && !username.trim().equals("")) {
			Volunteer v = new Volunteer("", "", username, "", "", "", null);
			//indexOf() utilizes equals() method within user class to locate account position
			int index = volunteerList.indexOf(v);
			if (index >= 0) {
				return volunteerList.get(index);
			}
			Scheduler s = new Scheduler(username, "", "", ""); 
			index = theScheduler.indexOf(s);
			if (index >= 0) {
				return theScheduler.get(index);
			}
			LMA l = new LMA("", username, "", "", ""); 
			index = lmaList.indexOf(l);
			if (index >= 0) {
				return lmaList.get(index);
			}
		}
		return null;
	}
	
	/**
	 * This method verify the password to the account for login purpose
	 * 
	 * @param username the username
	 * @param password the pasword
	 * @return boolean indicate whether the password is correct for that account
	 */
	public boolean verifyPassword(String username, String password) {
		User temp = this.getAccount(username);
		if (temp.getPassword().equals(password)) {
			return true;
		}
		return false;
	}
	
	/**
	 * This method allow for the removal of account base on the username, if the username does not
	 * exist within the user account database, then it simply does nothing
	 * 
	 * @param username the account with username to remove from account database
	 */
	public void deleteAccount(String username) {
		if (username != null && !username.trim().equals("")) {
			Volunteer v = new Volunteer("", "", username, "", "", "", null);
			int index = volunteerList.indexOf(v);
			if (index >= 0) {
				volunteerList.remove(index);
			}
			Scheduler s = new Scheduler(username, "", "", ""); 
			index = theScheduler.indexOf(s);
			if (index >= 0) {
				theScheduler.remove(index);
			}
			LMA l = new LMA("", username, "", "", ""); 
			index = lmaList.indexOf(l);
			if (index >= 0) {
				lmaList.remove(index);
			}
		}		
	}
	
	/**
	 * This method return a string that has all the accounts description
	 */
	public String toString() {
		String temp = "";
		for(Volunteer x : volunteerList) {
			temp += "Volunteer,," + x;
		}
		for(LMA y : lmaList) {
			temp += "LMA,," + y;
		}
		for(Scheduler z : theScheduler) {
			temp += "Scheduler,," + z;
		}
		return temp;
	}
	
	/**
	 * This method allow for retrieval of list of Volunteer accounts
	 * 
	 * @return an ArrayList of Volunteer
	 */
	public ArrayList<Volunteer> getVolunteerList() {
		return volunteerList;
	}

	/**
	 * This method allow for retrieval of list of LMA accounts
	 * 
	 * @return an ArrayList of LMA
	 */
	public ArrayList<LMA> getLmaList() {
		return lmaList;
	}

	/**
	 * This method allow for retrieval of list of scheduler accounts
	 * 
	 * @return an ArrayList of scheduler account
	 */
	public ArrayList<Scheduler> getTheScheduler() {
		return theScheduler;
	}

}
